var lesImages  = new Array();
var cpt        = 0;
var iter       = -1;
var pageHeight = 0;
var pageWidth  = 0;

function $(id)
{
    return document.getElementById(id);
}

window.onload = function()
{
    var de     = document.documentElement;
    pageWidth  = window.innerWidth     || 
                 self.innerWidth       || 
                 (de&&de.clientWidth)  || 
                 document.body.clientWidth;
    pageHeight = window.innerHeight    || 
                 self.innerHeight      || 
                 (de&&de.clientHeight) || 
                 document.body.clientHeight;
    pageWidth  = pageWidth  - 2;
    pageHeight = pageHeight - 55;
    
    $('image').style.height = pageHeight;
    $('image').style.width  = pageWidth;
    startDiapo();
    
    $('suiv').onclick = $('i').onclick = function()
    {
        pause();
        diapoSuivante();
    }
    
    $('prec').onclick = function()
    {
        pause();
        diapoPrec();
    }    
   
}

var isPause = false;

function cptAffiche(n)
{
    $('cpt').innerHTML = n;
}

function startDiapo()
{
    lesImages[0].affiche();
    diapoSuivante();    
}

function cptDown()
{
    cptAffiche(6);
    t5 = setTimeout('cptAffiche(5)',1000);
    t4 = setTimeout('cptAffiche(4)',2000);
    t3 = setTimeout('cptAffiche(3)',3000);
    t2 = setTimeout('cptAffiche(2)',4000);
    t1 = setTimeout('cptAffiche(1)',5000);
    t0 = setTimeout('cptAffiche(0)',6000);   
}

var duree = 5*1000; // 5 secondes
var t5,t4,t3,t2,t1,t0;
diapoSuivante = function()
{
    iter++;
    if(iter >= cpt) iter = 0;
    lesImages[iter].affiche();
    lesImages[(iter+1)%cpt].load();
    
    if(!isPause)
    {
        //cptDown();                  
        cptAffiche('<img src="plugin/diapo/images/pause.png" alt="pause" />');
        td = setTimeout('diapoSuivante()',duree);
        $('cpt').onclick = function()
        {
            pause();
        } 
    }
}

diapoPrec = function()
{
    iter--;
    if(iter < 0) iter = cpt-1;
    lesImages[iter].affiche();
    //lesImages[(iter-1)%cpt].load();
    
    if(!isPause)
    {
        cptDown();                  
        td = setTimeout('diapoPrec()',duree);
        $('cpt').onclick = function()
        {
            pause();
        } 
    }
}

pause = function()
{
    clearTimeout(t5);
    clearTimeout(t4);
    clearTimeout(t3);
    clearTimeout(t2);
    clearTimeout(t1);
    clearTimeout(t0);
    clearTimeout(td);       
    isPause = true;        
    cptAffiche('<img src="plugin/diapo/images/play.png" alt="play" />'); 
    $('cpt').onclick = function()
    {
        isPause = false;
        diapoSuivante();
    }        
}

function jImage(i,n,d,x,y)
{
    this.nom      = n;
    this.desc     = d;
    this.id       = i;
    this.height   = y;
    this.width    = x;
}

jImage.prototype.load = function()
{
    tmp = new Image();
    tmp.src = 'see.php?idItem='+this.id;
}

jImage.prototype.calcul = function()
{
    if (this.width > pageWidth)
    {
        this.height = this.height * (pageWidth / this.width); 
        this.width  = pageWidth; 
        if (this.height > pageHeight)
        { 
            this.width  = this.width * (pageHeight / this.height); 
            this.height = pageHeight; 
        }
    } 
    else if (this.height > pageHeight)
    { 
        this.width  = this.width * (pageHeight / this.height); 
        this.height = pageHeight; 
        if (this.width > pageWidth)
        { 
            this.height = this.height * (pageWidth / this.width); 
            this.width  = pageWidth;
        }
    }
}

jImage.prototype.affiche = function()
{
    this.calcul();
    $('nom').innerHTML  = this.nom;
    $('desc').innerHTML = this.desc;
    with($('i'))
    {        
        src              = 'see.php?idItem='+this.id;
        style.height     = this.height;
        style.width      = this.width;
        style.marginTop  = Math.floor((pageHeight - this.height) / 2);
        style.marginLeft = Math.floor((pageWidth  - this.width ) / 2);
    }
}



